package com.example.func.demos.web;

import cn.hutool.extra.spring.SpringUtil;
import com.example.func.annotation.RequestLog;
import com.example.func.dto.request.UserRequest;
import com.example.func.dto.response.ApiResponse;
import com.example.func.dto.response.UserResponse;
import com.example.func.enums.OpType;
import com.example.func.enums.Scenario;
import com.example.func.service.UserService;
import com.example.func.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户管理控制器
 * 演示如何使用DTO进行请求参数接收和响应数据返回
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    
    /**
     * 使用Hutool的SpringUtil获取UserService实例
     */
    private UserService getUserService() {
        return SpringUtil.getBean(UserService.class);
    }
    
    /**
     * 创建用户
     * 使用UserRequest接收请求参数，使用ApiResponse包装返回数据
     */
    @PostMapping("/create")
    public ApiResponse<UserResponse> createUser(@Valid @RequestBody UserRequest userRequest) {
        UserResponse userResponse = getUserService().createUser(userRequest);
        return ApiResponse.success("用户创建成功", userResponse);
    }
    
    /**
     * 获取用户列表
     * 返回用户VO列表，包含计算字段
     */
    @GetMapping("/list")
    @RequestLog(module = "用户列表查询模块")
    public ApiResponse<List<UserVO>> getUserList() {
        List<UserVO> userList = getUserService().getUserList();
        return ApiResponse.success(userList);
    }
    
    /**
     * 根据ID获取用户详情
     */
    @GetMapping("/{id}")
    @RequestLog(module = "用户详情查询模块")
    public ApiResponse<UserResponse> getUserById(@PathVariable Long id) {
        UserResponse userResponse = getUserService().getUserById(id);
        if (userResponse == null) {
            return ApiResponse.error(404, "用户不存在");
        }
        return ApiResponse.success(userResponse);
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/{id}")
    public ApiResponse<UserResponse> updateUser(@PathVariable Long id, @Valid @RequestBody UserRequest userRequest) {
        UserResponse userResponse = getUserService().updateUser(id, userRequest);
        if (userResponse == null) {
            return ApiResponse.error(404, "用户不存在");
        }
        return ApiResponse.success("用户更新成功", userResponse);
    }
    
    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    @RequestLog(module = "用户删除模块")
    public ApiResponse<Void> deleteUser(@PathVariable Long id) {
        boolean deleted = getUserService().deleteUser(id);
        if (!deleted) {
            return ApiResponse.error(404, "用户不存在");
        }
        return ApiResponse.success("用户删除成功", null);
    }
    
    /**
     * 根据姓名查询用户
     */
    @GetMapping("/search/name")
    @RequestLog(module = "用户姓名查询模块")
    public ApiResponse<List<UserVO>> getUserByName(@RequestParam String name) {
        List<UserVO> userList = getUserService().getUserByName(name);
        return ApiResponse.success(userList);
    }
    
    /**
     * 根据年龄范围查询用户
     */
    @GetMapping("/search/age")
    @RequestLog(module = "用户年龄查询模块")
    public ApiResponse<List<UserVO>> getUserByAgeRange(
            @RequestParam Integer minAge, 
            @RequestParam Integer maxAge) {
        List<UserVO> userList = getUserService().getUserByAgeRange(minAge, maxAge);
        return ApiResponse.success(userList);
    }
}
